<template>
	<view class="body">
		<input type="password" v-model="oldpassword"
		class="uni-input common-input" placeholder="输入旧密码"/>
		<input type="password" v-model="newpassword"
		class="uni-input common-input" placeholder="输入新密码"/>
		<input type="password" v-model="renewpassword"
		class="uni-input common-input" placeholder="再次输入新密码"/>
		<button class="btn" style="margin-top: 50upx;" @click="submit" type="primary">确认修改</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				user:{},
				oldpassword:'',
				newpassword:'',
				renewpassword:'',
				disable:true,
				loading:false
			}
		},
		onLoad(res){
			var that = this;
			this.id = res.id;
			this.$uni.request({
				url:'/user/getUserById'
			}).then((r)=>{
				that.user = r.user
			})
		},
		methods: {
			//验证层
			check(){
				if(!this.oldpassword || this.oldpassword == ''){
					uni.showToast({
						title:'旧密码不能为空',
						icon:'none'
					});
					return false;
				}
				if(!this.newpassword || this.newpassword == ''){
					uni.showToast({
						title:'新密码不能为空',
						icon:'none'
					});
					return false;
				}
				if(!this.renewpassword || this.renewpassword == ''){
					uni.showToast({
						title:'请再次输入新密码',
						icon:'none'
					});
					return false;
				}
				if(this.newpassword.length < 3 || this.newpassword.length >11){
					uni.showToast({
						title:'密码长度错误，请输入3-11位新密码',
						icon:'none'
					});
					return false;
				}
				if(this.newpassword !== this.renewpassword){
					uni.showToast({
						title:'新密码两次输入不同',
						icon:'none'
					});
					return false;
				}
				return true;
			},
			//提交
			submit(){
				if(!this.check()){
					return;
				}else{
					
					this.user.password = this.oldpassword;
					// this.user.newpassword = this.newpassword;
				}
				this.$uni.request({
					url: '/user/isRightPwd',
					method: 'post',
					data: this.user
				}).then((res) => {
					if (res.code == 200){
						this.user.password = this.newpassword;
						this.$uni.request({
							url: '/user/updatePwd',
							method: 'post',
							data: this.user
						}).then((res) => {
							if (res.code == 200){
								this.$toast.success('修改成功')
								console.log(res)
								uni.reLaunch({
									url: '../user/user_logined'
								})
							} else
								this.$toast.error(res.msg)
						})
						
					} else
						this.$toast.error(res.msg)
				})
				
				
				// this.$uni.request({
				// 	url: '/user/updatePwd',
				// 	method: 'post',
				// 	data: this.user
				// }).then((res) => {
				// 	if (res.code == 200){
				// 		this.$toast.success('修改成功')
				// 		console.log(res)
				// 		uni.reLaunch({
				// 			url: '../user/user_logined'
				// 		})
				// 	} else
				// 		this.$toast.error(res.msg)
				// })
			
			}
			
		}
	}
</script>

<style>
	@import url("/static/css/uni.css");
	@import url("/static/css/form.css");
	.body{
		padding: 0 20upx;
		padding-top: 50upx;
	}
</style>
